﻿using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using ent;

public partial class controlpanel_list_items_crud : System.Web.UI.Page {
    public String URL = ApplicationCommon.getPageThis();

    public String TITLE = String.Empty;
    public String LIST_CATEGORY_ID = String.Empty;

    private String _qsMode = String.Empty;
    private String _qsReturn = String.Empty;
    private int _qsId = 0;
    private int _qsIdCategory = 0;

    private TableUsers t1 = new TableUsers();
    private TableLists t3 = new TableLists();
    private ServiceLists s1 = new ServiceLists();

    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        this.Page.Header.DataBind();
    }
    protected void Page_Load(Object sender, EventArgs e) {
        _qsMode = Request.QueryString["mode"];
        _qsReturn = Request.QueryString["return"];
        _qsId = System.Convert.ToInt32(Request.QueryString["id"]);
        _qsIdCategory = System.Convert.ToInt32(Request.QueryString["category"]);
        getRequestJavaScript();

        // Is category available.
        if(_qsIdCategory <= 0) {
            lblError.Text = "Error: List not found from category.";
            return;
        }

        // Get list category.
        Hashtable p1 = new Hashtable();
        p1.Add(TableLists.TBL__lists__list_id, System.Convert.ToInt32(_qsIdCategory));
        DataRow dr1 = t3.getCategory(p1);
        TITLE = (String)dr1[TableLists.TBL__lists__list_name];
        LIST_CATEGORY_ID = _qsIdCategory.ToString();
        Page.Title = TITLE + " Management";

        // Mode.
        if(String.Equals(_qsMode, "add")) {
            viewAdd();
        } else if(String.Equals(_qsMode, "edit") && _qsId > 0) {
            viewEdit();
        } else if(String.Equals(_qsMode, "trash")) {
            viewTrash();
        } else {
            viewCrud();
        }
    }

#region Methods

    /// <summary>Get HTTP request from JavaScript postback.</summary>
    protected void getRequestJavaScript() {
        if(!Page.IsPostBack) {return;}

        String eventName = Request.Form["__EVENTTARGET"];
        String eventArgument = Request.Form["__EVENTARGUMENT"];

        if(String.Equals(eventName, "onMode")) {
            // Get multiple values from serialized string.
            System.Collections.Specialized.NameValueCollection aryEventArguments = System.Web.HttpUtility.ParseQueryString(eventArgument);
            // Set mode by query string.
            if(String.Equals(aryEventArguments["action"], "add")) {
                Hashtable qs1 = new Hashtable();
                qs1.Add("mode", "add");
                qs1.Add("category", _qsIdCategory);
                Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
            } else if(String.Equals(aryEventArguments["action"], "edit")) {
                if(!ApplicationCommon.isEmpty(aryEventArguments["id"])) {
                    Hashtable qs1 = new Hashtable();
                    qs1.Add("mode", "edit");
                    qs1.Add("category", _qsIdCategory);
                    qs1.Add("id", System.Convert.ToInt32(aryEventArguments["id"]));
                    Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
                }
            } else if(String.Equals(aryEventArguments["action"], "trash")) {
                Hashtable qs1 = new Hashtable();
                qs1.Add("mode", "trash");
                qs1.Add("category", _qsIdCategory);
                Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
            } else {
                Hashtable qs1 = new Hashtable();
                qs1.Add("category", _qsIdCategory);
                Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
            }
        }
    }
    /// <summary>Init form input values.</summary>
    protected void initFormInputValues() {
        // First load data for manipulation before postback.
        if(!Page.IsPostBack) {
            // Get list: categories.
            ucListItem.prop_ddlCategory.DataSource = t3.getCategories(null);
            ucListItem.prop_ddlCategory.DataTextField = TableLists.TBL__lists__list_name;
            ucListItem.prop_ddlCategory.DataValueField = TableLists.TBL__lists__list_id;
            ucListItem.prop_ddlCategory.DataBind();
        }
    }
    /// <summary>View in CRUD mode.</summary>
    protected void viewCrud() {
        Master.prop_plhHeadYui.Visible = true;
        plhClientScript.Visible = true;
        plhJavaScriptDataTableCrud.Visible = true;
        plhCrud.Visible = true;
    }
    /// <summary>View in add mode.</summary>
    protected void viewAdd() {
        plhAdd.Visible = true;
        plhForm.Visible = true;
        // Init form input values.
        initFormInputValues();
        // First load data for manipulation before postback.
        if(!Page.IsPostBack) {
            // List type controls goes here.
        }
    }
    /// <summary>View in edit mode.</summary>
    protected void viewEdit() {
        plhEdit.Visible = true;
        plhForm.Visible = true;
        DataRow dr1 = t3.getItem(_qsId);
        // Init form input values.
        initFormInputValues();
        // First load data for manipulation before postback.
        if(!Page.IsPostBack) {
            ucListItem.prop_ddlCategory.Items.FindByValue(dr1[TableLists.TBL__lists__list_category_id].ToString()).Selected = true;
            ucListItem.prop_txtSymbol.Text = (String)dr1[TableLists.TBL__lists__list_symbol];
            ucListItem.prop_txtName.Text = (String)dr1[TableLists.TBL__lists__list_name];
            ucListItem.prop_txtValue.Text = dr1[TableLists.TBL__lists__list_value].ToString();
            ucListItem.prop_txtDescription.Text = (String)dr1[TableLists.TBL__lists__list_description];
            ucListItem.prop_txtOrder.Text = dr1[TableLists.TBL__lists__list_order].ToString();
        }
    }
    /// <summary>View in trash mode.</summary>
    protected void viewTrash() {
        Master.prop_plhHeadYui.Visible = true;
        plhTrash.Visible = true;
        plhClientScript.Visible = true;
        plhJavaScriptDataTableEdit.Visible = true;
    }
    /// <summary>Return user from (previous page).</summary>
    protected bool returnUserFrom() {
        String _qsReturn = Request.QueryString["return"];
        if(!ApplicationCommon.isEmpty(_qsReturn)) {
            Response.Redirect(HttpUtility.UrlDecode(_qsReturn));
        }
        return false;
    }

#endregion

#region Events

    protected void btnFormOkay_Click(Object sender, EventArgs e) {
        if(plhAdd.Visible) {
            Hashtable p1 = new Hashtable();
            p1.Add(TableLists.TBL__lists__list_category_id, ucListItem.prop_ddlCategory.SelectedValue);
            p1.Add(TableLists.TBL__lists__list_symbol, ucListItem.prop_txtSymbol.Text);
            p1.Add(TableLists.TBL__lists__list_name, ucListItem.prop_txtName.Text);
            p1.Add(TableLists.TBL__lists__list_value, System.Convert.ToInt32(ucListItem.prop_txtValue.Text));
            p1.Add(TableLists.TBL__lists__list_description, ucListItem.prop_txtDescription.Text);
            p1.Add(TableLists.TBL__lists__list_order, System.Convert.ToInt32(ucListItem.prop_txtOrder.Text));
            t3.dynamicSqlInsert(p1, TableLists.TBL__lists);
        } else if(plhEdit.Visible) {
            Hashtable p2 = new Hashtable();
            p2.Add(TableLists.TBL__lists__list_category_id, ucListItem.prop_ddlCategory.SelectedValue);
            p2.Add(TableLists.TBL__lists__list_symbol, ucListItem.prop_txtSymbol.Text);
            p2.Add(TableLists.TBL__lists__list_name, ucListItem.prop_txtName.Text);
            p2.Add(TableLists.TBL__lists__list_value, System.Convert.ToInt32(ucListItem.prop_txtValue.Text));
            p2.Add(TableLists.TBL__lists__list_description, ucListItem.prop_txtDescription.Text);
            p2.Add(TableLists.TBL__lists__list_order, System.Convert.ToInt32(ucListItem.prop_txtOrder.Text));
            t3.dynamicSqlUpdate(p2, TableLists.TBL__lists, TableLists.TBL__lists___PK + " = " + _qsId);
        }
        if(!returnUserFrom()) {
            Hashtable qs1 = new Hashtable();
            qs1.Add("category", _qsIdCategory);
            Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
        }
    }
    protected void btnFormCancel_Click(Object sender, EventArgs e) {
        if(!returnUserFrom()) {
            Hashtable qs1 = new Hashtable();
            qs1.Add("category", _qsIdCategory);
            Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
        }
    }

#endregion

}